---
title: ''
output: pdf_document
geometry: "top=1in, bottom=1in, left=1in, right=1in"
header-includes:
- \usepackage{caption}
- \usepackage{lscape}
---

\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}

\captionsetup[table]{labelformat=empty}


```{r setup, include=FALSE, message=FALSE}

# Package setup
if (!require("pacman")) install.packages("pacman", repos = "http://cran.us.r-project.org"); library(pacman);
pacman::p_load(yaml, readr, tidyr, argparse, stringr, here, glue, dplyr, knitr, kableExtra, forcats);

# Working directory and command line argument setup
task_output = "other_outcomes_table"
current_script = paste0("generate_tables_and_figures/gen_", task_output, "/src/gen_", task_output, ".Rmd")
here::i_am(current_script)
source(here("R", "project_functions.R"))
task_dir = here(dirname(dirname(file.path(current_script))))
knitr::opts_knit$set(root.dir = task_dir)
cl_args <- parse_make_args(c(
  "RMD_FILE",
  "RESULTS",
  "OUTCOME_LABELS_FILE",
  "CONFIG_FILE",
  "TABLE_CONFIG",
  "TABLE_FNS",
  "OUT_DIR"
), inter_active = TRUE, task_dir = task_dir)

knitr_setup()
  
```


```{r}

results <- read_csv(cl_args$RESULTS, show_col_types = F)

ylabels <- read_csv("../hand/baseline_outcome_labels.csv", show_col_types = F) %>%
  rename(outcome = value)

config <- read_yaml(cl_args$CONFIG_FILE)

source('../R/table_functions.R')
table_config <- read_yaml('../hand/tables_config.yaml')

```


```{r}

beta_se_digits <- table_config$digits$beta_se

# Filter to window and outcomes
outcomes_table <- results %>% 
  filter(window == config$window) %>%
  filter(outcome %in% config$outcomes) %>%
  mutate(outcome = fct_relevel(outcome, config$outcomes)) %>%
  arrange(outcome) %>%
  mutate(outcome_order = row_number()) %>%
  left_join(ylabels, by = "outcome") %>%
  mutate(label = case_when(label == "Other (Non-Shooting) Violent Victimizations" ~ "Non-Shooting Violent Victimizations",
                           TRUE ~ label)) %>%
  mutate(across(.cols = where(is.numeric),
                .fns = ~case_when(abs(.x) > 1 ~ fmt(.x, decimals = 1),
                                                    TRUE ~ fmt(.x))))

# Stack standard errors under betas in parentheses
beta_se_stack <- stack_stderrs_under_betas(outcomes_table)

# Make table
table <- beta_se_stack %>%
  select(all_of(names(config$columns))) %>% 
  mutate(label = case_when(grepl("Days in ", label) & control_mean < 1 ~ gsub("Days in ", "Incarcerated in ", label),
                           TRUE ~ label)) %>% 
  kable(booktabs = table_config$booktabs, 
        longtable = table_config$longtable,
        col.names = unlist(config$columns),
        align = c(table_config$alignment$text, 
                  rep(table_config$alignment$numeric, 
                      length(config$columns) - 1)),
        linesep = "") %>% 
  kable_styling(font_size = table_config$font_size) %>%
  group_rows("Other Arrest & Victimization", 1, 19) %>%
  group_rows("\tCounts", 1, 9) %>%
  group_rows("\tIndicators", 11, 19) %>%
  group_rows("Incarceration & Incapacitation", 21, 39) %>%
  group_rows("\tCounts", 21, 31) %>%
  group_rows("\tIndicators", 33, 39) %>%
  column_spec(1, width = '18em') %>%
  column_spec(2:length(config$columns), 
              width = config$col_width$numeric, 
              latex_valign = "m") %>% 
  add_indent(25:27) %>% 
  add_indent(29:31, level_of_indent = 2) %>% 
  add_indent(35:36) %>% 
  add_indent(37:40, level_of_indent = 2)

table

# SAVE

timestamp <- get_timestamp()
task_output <- "outcomes_other" # To match what is on overleaf already
writeLines(table, paste0(cl_args$OUT_DIR, "/", task_output, "_up_to_date.tex"))
writeLines(table, paste0(cl_args$OUT_DIR, "/", task_output, "_", timestamp, ".tex"))

```
